<?php

namespace app\admin\controller\mul;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Mission extends Backend
{
    
    /**
     * MulMission模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('MulMission');

    }



    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where=array();
            $where['is_delete']=0;
            $inputs=input();
            $input = json_decode($inputs['filter'], true);
            if(!empty($input['id'])){
                $where['id']=$input['id'];
            }
            if(!empty($input['title'])){
                $where['title']=array('like','%'.$input['title'].'%');
            }
            if(!empty($input['status'])){
                $where['status']=$input['status'];
            }
            if(!empty($input['goods'])){
                $where['goods']=array('like','%'.$input['goods'].'%');
            }
            if(!empty($input['boss_name'])){
                $where['boss_name']=array('like','%'.$input['boss_name'].'%');
            }
            if(!empty($input['boss_tel'])){
                $where['boss_tel']=$input['boss_tel'];
            }
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as &$l){
                if($l['status']==1){
                    $l['status']='已发布';
                }elseif($l['status']==2){
                    $l['status']='已结束';
                }elseif($l['status']==3){
                    $l['status']='已取消';
                }
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        $btns = array('refresh');

        $this->view->assign('btns', $btns);
        return $this->view->fetch();
    }
    //详情
    public function lists($ids=''){
        if($ids){
            $where=array();
            $list = DB::name('MulMissionEmployee')
                ->where(['mission_id'=>$ids])
                ->order('id DESC')
                ->select();
            $data=array(
                '1'=>'已报名',
                '2'=>'平台已确认',
                '3'=>'金主已确认',
                '4'=>'任务未完成',
                '5'=>'任务落选',
                '6'=>'已结账'
            );
            foreach($list as &$l){
                $l['status']=$data[$l['status']];
                $l['mission_id']=DB::name('MulMission')->where('id='.$l['mission_id'])->value('title');
                $l['user_id']=DB::name('Users')->where('user_id='.$l['user_id'])->value('nickname');
            }
            $this->assign('list',$list);
            $this->assign('ids',$ids);
            return $this->view->fetch();
        }
    }
    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids)
        {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds))
            {
                $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();
            $count = 0;
            foreach ($list as $k => $v)
            {
                $count += $v->save(['is_delete'=>'1']);
            }
            if ($count)
            {
                $this->success();
            }
            else
            {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
    /**
     * 手机连接
     */
    public function url($ids){
        if($ids){
            $id=alias_encode($ids);
            $url = 'http://moments.taoxiansheng.com/?#/service/talent_pool/resume_detail/'.$id;
            $this->view->assign('mobile_url', $url);
            return $this->view->fetch();
        }
    }
    /**
     * 已结束
     */
    public function end($id){
        $mission=$this->model
            ->where('id='.$id.' and status=1')
            ->find();
        if(empty($mission)){
            return array('code'=>-200,'msg'=>'该信息有误或该信息已结束');
        }
        $count=DB::name('MulMissionEmployee')->where('mission_id='.$id)->find();
        if(empty($count)){
            return array('code'=>-200,'msg'=>'暂无信息');
        }
        $employee=DB::name('MulMissionEmployee')->where(['mission_id'=>$id,'status'=>2])->whereor(['status'=>3])->find();
        if(isset($employee)){
            return array('code'=>-200,'msg'=>'该信息有金主未确认或未结账信息');
        }
        $re=$mission->save(['status'=>2]);
        if($re>0){
            return array('code'=>200,'msg'=>'该信息结束了');
        }
    }
}
